<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<!--
/* ==========================================
 * JGraphT : a free Java graph-theory library
 * ==========================================
 *
 * Project Info:  http://jgrapht.sourceforge.net/
 * Project Lead:  Barak Naveh (http://sourceforge.net/users/barak_naveh)
 *
 * (C) Copyright 2003, by Barak Naveh and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
/* ~~~~~~~~~~~~~~~~~~~
 * visualizations.html
 * ~~~~~~~~~~~~~~~~~~~
 * (C) Copyright 2003, by Barak Naveh and Contributors.
 *
 * Original Author:  Barak Naveh
 * Contributor(s):   -
 *
 * $Id: visualizations.html 677 2009-03-13 22:40:26Z perfecthash $
 *
 * Changes
 * ~~~~~~~
 * 03-Aug-2003 : Initial revision (BN);
 * 
 */
-->

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="Author" content="Barak Naveh">
  <meta name="keywords" content="JGraphT, Java graph library, graph theory, free, java, LGPL, open-source">
  <title>JGraphT visualization using JGraph</title>
</head>
<body>
<h1><font color="#cc3399">J</font>Graph<font color="#ffcc00">T</font>&nbsp;Visualizations<br>
<font size="5">via JGraph</font></h1>


<br>

<h2>THIS PAGE IS OUT OF DATE</h2>

Modern web browsers don't support applets directly, and <a href="https://github.com/jgrapht/jgrapht/issues/522">JGraphT is dropping support for JGraph and replacing it with JGraphX</a>.  If you'd like to help with making this page work again, <a href="https://github.com/jgrapht/jgrapht/issues/523">here is the relevant github issue</a>.

<h2>Demo Applet</h2>


<p>The following applet shows how a JGraphT graph can be visualized using 
<a href="http://www.jgraph.com/">JGraph</a>. Try to play and drag 
around the vertices and edges to get the feel of it.</p>


<p><font color="#7AA1E6" size="2">Note: Java 1.3 or above must be installed for 
this applet to work correctly.</font></p>

<br/>


<table align="center" border="1" bordercolor="#7AA1E6" style="border-collapse: collapse" >
  <tr><td>
	<applet width="550" height="330" 
        code="org._3pq.jgrapht.demo.JGraphAdapterDemo.class" 
		archive="./jgrapht-0.5.2.jar">
		<param name="bgcolor" value="#ffffff">
		
  		Java 2 Standard Edition v 1.3 or above is required for this applet.<br/>
		Download it from <a href="http://java.sun.com">http://java.sun.com</a>.
  	</applet>
  </td></tr>
</table>
<DIV style="TEXT-ALIGN: center">a JGraphT graph visualized using JGraph.</DIV>

<p>&nbsp;</p>


<h2>How it Works</h2>

<p>It's very simple: the JGraphT library comes with an 
adapter that makes JGraphT graphs compatible with JGraph. To visualize a JGraphT 
graph you just need to initialize JGraph via that adapter. </p>

<p>Example code:</p>

<p>
<pre><i><font color="#b22222">     // create a JGraphT graph</font></i>
     ListenableGraph g = <B><FONT color=#a020f0>new</FONT></B> ListenableDirectedGraph( DefaultEdge.class );

     <i><font color="#b22222">// create a visualization using JGraph, via the adapter</font></i>
      JGraph jgraph = <b><font color="#a020f0">new</font></b> JGraph( <b><font color="#a020f0">new</font></b> JGraphModelAdapter( g ) );
</pre>
</p>


<p>Is that all?!&nbsp; Yes, that's all. Any modification now made to the graph <code>g</code> will 
automatically be reflected by the JGraph component.</p>





<p>&nbsp;</p>





<h2>Source Code of the Applet</h2>



<p>The full source code of this demo is listed below and is also included in the 
JGraphT distribution (<a href="http://sourceforge.net/project/showfiles.php?group_id=86459">download now</a>).<br>
&nbsp;</p>



<div align="center">
  <center>



<table border="1" bordercolor="#7AA1E6" bgcolor="white" cellpadding="20" style="border-collapse: collapse" cellspacing="0">
  <tr>
    <td align="left">
    
<PRE><B><FONT color=#a020f0>package</FONT></B> org.jgrapht.demo;

<B><FONT color=#a020f0>import</FONT></B> java.awt.Color;
<B><FONT color=#a020f0>import</FONT></B> java.awt.Dimension;
<B><FONT color=#a020f0>import</FONT></B> java.awt.Rectangle;

<B><FONT color=#a020f0>import</FONT></B> java.util.HashMap;
<B><FONT color=#a020f0>import</FONT></B> java.util.Map;

<B><FONT color=#a020f0>import</FONT></B> javax.swing.JApplet;
<B><FONT color=#a020f0>import</FONT></B> javax.swing.JFrame;

<B><FONT color=#a020f0>import</FONT></B> org.jgraph.JGraph;
<B><FONT color=#a020f0>import</FONT></B> org.jgraph.graph.DefaultGraphCell;
<B><FONT color=#a020f0>import</FONT></B> org.jgraph.graph.GraphConstants;

<B><FONT color=#a020f0>import</FONT></B> org.jgrapht.ListenableGraph;
<B><FONT color=#a020f0>import</FONT></B> org.jgrapht.ext.JGraphModelAdapter;
<B><FONT color=#a020f0>import</FONT></B> org.jgrapht.graph.ListenableDirectedGraph;
<B><FONT color=#a020f0>import</FONT></B> org.jgrapht.graph.DefaultEdge;

<I><FONT color=#b22222>/**
 * A demo applet that shows how to use JGraph to visualize JGraphT graphs.
 *
 * @author Barak Naveh
 *
 * @since Aug 3, 2003
 */</FONT></I>
<B><FONT color=#a020f0>public</FONT></B> <B><FONT color=#a020f0>class</FONT></B> JGraphAdapterDemo <B><FONT color=#a020f0>extends</FONT></B> JApplet {
    <B><FONT color=#a020f0>private</FONT></B> <B><FONT color=#a020f0>static</FONT></B> <B><FONT color=#a020f0>final</FONT></B> Color     DEFAULT_BG_COLOR = Color.decode( <B><FONT color=#bc8f8f>"#FAFBFF"</FONT></B> );
    <B><FONT color=#a020f0>private</FONT></B> <B><FONT color=#a020f0>static</FONT></B> <B><FONT color=#a020f0>final</FONT></B> Dimension DEFAULT_SIZE = <B><FONT color=#a020f0>new</FONT></B> Dimension( 530, 320 );

    <I><FONT color=#b22222>// 
</FONT></I>    <B><FONT color=#a020f0>private</FONT></B> JGraphModelAdapter m_jgAdapter;

    <I><FONT color=#b22222>/**
     * @see java.applet.Applet#init().
     */</FONT></I>
    <B><FONT color=#a020f0>public</FONT></B> <B><FONT color=#a020f0>void</FONT></B> init(  ) {
        <I><FONT color=#b22222>// create a JGraphT graph
</FONT></I>        ListenableGraph g = <B><FONT color=#a020f0>new</FONT></B> ListenableDirectedGraph( DefaultEdge.class );

        <I><FONT color=#b22222>// create a visualization using JGraph, via an adapter
</FONT></I>        m_jgAdapter = <B><FONT color=#a020f0>new</FONT></B> JGraphModelAdapter( g );

        JGraph jgraph = <B><FONT color=#a020f0>new</FONT></B> JGraph( m_jgAdapter );

        adjustDisplaySettings( jgraph );
        getContentPane(  ).add( jgraph );
        resize( DEFAULT_SIZE );

        <I><FONT color=#b22222>// add some sample data (graph manipulated via JGraphT)
</FONT></I>        g.addVertex( <B><FONT color=#bc8f8f>"v1"</FONT></B> );
        g.addVertex( <B><FONT color=#bc8f8f>"v2"</FONT></B> );
        g.addVertex( <B><FONT color=#bc8f8f>"v3"</FONT></B> );
        g.addVertex( <B><FONT color=#bc8f8f>"v4"</FONT></B> );

        g.addEdge( <B><FONT color=#bc8f8f>"v1"</FONT></B>, <B><FONT color=#bc8f8f>"v2"</FONT></B> );
        g.addEdge( <B><FONT color=#bc8f8f>"v2"</FONT></B>, <B><FONT color=#bc8f8f>"v3"</FONT></B> );
        g.addEdge( <B><FONT color=#bc8f8f>"v3"</FONT></B>, <B><FONT color=#bc8f8f>"v1"</FONT></B> );
        g.addEdge( <B><FONT color=#bc8f8f>"v4"</FONT></B>, <B><FONT color=#bc8f8f>"v3"</FONT></B> );

        <I><FONT color=#b22222>// position vertices nicely within JGraph component
</FONT></I>        positionVertexAt( <B><FONT color=#bc8f8f>"v1"</FONT></B>, 130, 40 );
        positionVertexAt( <B><FONT color=#bc8f8f>"v2"</FONT></B>, 60, 200 );
        positionVertexAt( <B><FONT color=#bc8f8f>"v3"</FONT></B>, 310, 230 );
        positionVertexAt( <B><FONT color=#bc8f8f>"v4"</FONT></B>, 380, 70 );

        <I><FONT color=#b22222>// that's all there is to it!...
</FONT></I>    }


    <B><FONT color=#a020f0>private</FONT></B> <B><FONT color=#a020f0>void</FONT></B> adjustDisplaySettings( JGraph jg ) {
        jg.setPreferredSize( DEFAULT_SIZE );

        Color  c        = DEFAULT_BG_COLOR;
        String colorStr = <B><FONT color=#a020f0>null</FONT></B>;

        <B><FONT color=#a020f0>try</FONT></B> {
            colorStr = getParameter( <B><FONT color=#bc8f8f>"bgcolor"</FONT></B> );
        }
         <B><FONT color=#a020f0>catch</FONT></B>( Exception e ) {}

        <B><FONT color=#a020f0>if</FONT></B>( colorStr != <B><FONT color=#a020f0>null</FONT></B> ) {
            c = Color.decode( colorStr );
        }

        jg.setBackground( c );
    }


    <B><FONT color=#a020f0>private</FONT></B> <B><FONT color=#a020f0>void</FONT></B> positionVertexAt( Object vertex, <B><FONT color=#a020f0>int</FONT></B> x, <B><FONT color=#a020f0>int</FONT></B> y ) {
        DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );
        Map              attr = cell.getAttributes(  );
        Rectangle        b    = GraphConstants.getBounds( attr );

        GraphConstants.setBounds( attr, <B><FONT color=#a020f0>new</FONT></B> Rectangle( x, y, b.width, b.height ) );

        Map cellAttr = <B><FONT color=#a020f0>new</FONT></B> HashMap(  );
        cellAttr.put( cell, attr );
        m_jgAdapter.edit( cellAttr, null, null, null, null );
    }
}</PRE>
    
    </td>
  </tr>
</table>



  </center>
</div>



<p>&nbsp;</p>

<hr noshade size="2" style="background-color:#7AA1E6;color:#7AA1E6;border-width: thin none none;">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="100%">
  <tr>
    <td width="10%"align="left"><a href="http://validator.w3.org/check/referer"><img
          src="http://www.w3.org/Icons/valid-html401" border="0"
          alt="Valid HTML 4.01!" height="31" width="88"/></a></td>
          
    <td width="80%"align="center"><small>
		  &copy; Copyright 2003, by Barak Naveh and Contributors. All rights reserved.</small></td>
	    
	<td width="10%" align="right">
<a href="http://sourceforge.net/projects/jgrapht"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=86459&type=12" width="120" height="30" border="0" alt="Get JGraphT at SourceForge.net. Fast, secure and Free Open Source software downloads" /></a>
</td>
  </tr>
</table>
<br/>



</body>
</html>
